package com.hzx.controller;

import com.hzx.pojo.ComPicture;
import com.hzx.pojo.Commodity;
import com.hzx.service.ComPictureService;
import com.hzx.service.CommodityService;
import com.hzx.service.ShopReleaseService;
import com.hzx.until.FileUpload;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ModifyReleaseController {


    private static String COMPICTUREPATH = "F:\\newideafile\\dbpj\\src\\main\\resources\\static\\img\\ComPicture\\";
    private static String ComPIC = "\\img\\ComPicture\\";

    @Autowired
    private ShopReleaseService shopReleaseService;

    @Autowired
    private ComPictureService comPictureService;

    @Autowired
    private CommodityService commodityService;

    public Integer releaseindexid; // 单独一件商品的 id

    @GetMapping("/modifyrelease.html")
    public String modifyReleaseController(Model model,@RequestParam  Integer id) {

        releaseindexid = id;
        Commodity commodity = commodityService.selectByid(id);
        model.addAttribute(commodity);
        return "modifyrelease";
    }

    @RequestMapping(value = "/modifyrelease", method = RequestMethod.POST)
    public String modifyRelease(@RequestParam String name,
                                @RequestParam int count,
                                @RequestParam String price,
                                @RequestParam String o_price,
                                @RequestParam String take_way,
                                @RequestParam String describe,
                                @RequestParam Integer kid,
                                @RequestPart("img") MultipartFile file,
                                HttpServletRequest request,
                                Model model) {

        Commodity commodity = new Commodity();
        ComPicture comPicture = new ComPicture();
        commodity.setId(releaseindexid);


        if(!StringUntil.isNullOrEmpty(file)){
            String filename = releaseindexid+".jpg";
            comPicture.setPicture(ComPIC+filename);
            FileUpload.File_upload(file,COMPICTUREPATH,filename) ;
        }

        if (!StringUntil.isNullOrEmpty(count)) {
            commodity.setCount(count);
        }
        if (!StringUntil.isNullOrEmpty(name)) {
            commodity.setName(name);
        }
        if (!StringUntil.isNullOrEmpty(kid)) {
            commodity.setKid(kid);
        }

        if (!StringUntil.isNullOrEmpty(price)) {
            commodity.setPrice(Double.parseDouble(price));
        }
        if (!StringUntil.isNullOrEmpty(o_price)) {
            commodity.setO_price(Double.parseDouble(o_price));
        }
        if (!StringUntil.isNullOrEmpty(take_way)) {
            commodity.setTake_way(take_way);
        }
        if (!StringUntil.isNullOrEmpty(describe)) {
            commodity.setDescribe(describe);
        }

        int ans = shopReleaseService.updateByPrimaryKeySelective(commodity); // 通过商品的 id 修改 商品信息
        if (ans == 1 ) {
            model.addAttribute(commodity);
            return "modifyrelease";
        } else {
            return "修改失败";
        }
    }

    private boolean uploadfile(MultipartFile file, Integer commoidtyId) {
        String comPicName = commoidtyId + ".jpg";
        ComPicture comPicture = new ComPicture();
        comPicture.setPicture(comPicName);
        comPicture.setCid(commoidtyId);
        FileUpload.File_upload(file, COMPICTUREPATH, comPicName);
        Integer deleteOriginCompic = comPictureService.deleteById(commoidtyId);  // 删除数据库中原有的图片
        int ans = comPictureService.insertSelective(comPicture);
        return true;
    }

}
